<?php

namespace app\common\model;

use think\Model;

class Lists extends Model
{
    // 配置
    public static $configure = [
        '高顶',
        '平顶',
        '全隔断',
        '半隔断',
        '无隔断',
        '双开门',
        '单开门',
        '商务桌',
        '电视',
        '冰箱',
        '对开门',
        '云梯',
        '后挂备胎',
        '车顶帐篷'
    ];
    // 座位
    public static $seat = [
        4 => '4座',
        5 => '5座',
        6 => '6座',
        7 => '7座',
        9 => '9座',
    ];
    // 驱动
    public static $drive = [
        '前置后驱',
        '后置后驱',
        '四轮驱动'
    ];
    // 价格
    public static $price = [
        '60万以下',
        '60-80万',
        '80-100万',
        '100-120万',
        '120万以上'
    ];

    // 排量
    public static $pailiang = [
        '2.0L',
        '2.3L',
        '2.5L',
        '3.0L',
        '3.5L',
        '5.4L',
        '6.0L',
        '其他'
    ];

    // 亮点
    public static $liang_dian = [
        '双拼奇妙撞色视感',
        '双边智能防夹电动侧门',
        '全新Airmatic四轮空气悬挂系统',
        '136度超曲OLED环幕影像系统',
        '沉浸式视觉感官盛宴',
        '总裁限量版航空级首长席',
        '极智隐藏式对坐设计',
        '管家式智能操控面板',
        '悬浮式全彩极致大屏中控',
        '长条直列式迈巴赫宫格顶灯',
        '360度自由环绕式空调香氛设计',
        '“云境”八重空气智能净化系统',
        '智能钥匙远程一键启动',
        '车尾全新空气力学翼型扰流设计',
        '极致动力调教',
        '轿车化灵动前脸',
        '轻奢优雅车身腰线',
        '点金色晕染虎斑檀木',
        '柏林之声环绕立体声音响',
        '三联电动沙发床一键平躺',
        '厚菱形格包覆工艺',
        '旖旎奢享行宫',
        '迈巴赫经典设计元素',
        '阿尔坎塔拉麂皮绒顶',
        '流水型塑钢高顶车身',
        '明星级出行礼遇',
        'COMAND智能娱乐驾控系统',
        'Nappa真皮包覆三幅式方向盘',
        '一体式弧面中控',
        '内饰设计极尽空间美学'
    ];

    // 返回图片地址
    public function getManyImagesArrAttr($value, $data)
    {
        $arr = [];
        $many_images = $data['many_images'];

        if (is_string($many_images) && !empty($many_images)) {
            $arr = explode(',', $many_images);
        }
        return $arr;
    }
    // 获取亮点
    public function getLiangDianArrAttr($value, $data)
    {
        $arr = [];
        $many_images = $data['liangdian'];

        if (is_string($many_images) && !empty($many_images)) {
            $arr = explode('/', $many_images);
        }
        return $arr;
    }

    public function getCateNameAttr($value,$data)
    {
        $name = Cate::where('id',$data['pid'])->value('name');
        return $name;
    }

    public function getHotTextAttr($value, $data)
    {
        $status = [0 => '热门', 1 => '非热门'];
        return $status[$data['hot']];
    }

    /**
     * 数据验证
     * @param $data
     * @return false
     */
    public function verify($data)
    {
        $seat = $data['zuowei'];
        $drive = $data['qudong'];
        $price = $data['jiage'];

        if (!array_key_exists($seat, Lists::$seat)) {
            $this->error = '座位信息错误！';
            return false;
        }
        if (!array_key_exists($drive, Lists::$drive)) {
            $this->error = '驱动信息错误！';
            return false;
        }
        if (!array_key_exists($price, Lists::$price)) {
            $this->error = '价格信息错误！';
            return false;
        }

        $peizhi = $this->checked($data, 'peizhi', Lists::$configure);
        $liangdian = $this->checked($data, 'liangdian', Lists::$liang_dian);
        $data['peizhi'] = $peizhi;
        $data['liangdian'] = $liangdian;
        $data['qudong'] = Lists::$drive[$drive];
        $data['jiage'] = Lists::$price[$price];

        return $data;
    }

    // 获取复选框的值
    protected function checked($data, $filed, $arr, $count = 50)
    {
        $str = '';
        for ($i = 0; $i <= $count; $i++) {
            if (isset($data[$filed . "[$i]"])) {
                $str .= $i . '/';
            }
        }
        $str = trim($str,'/');
        $new_str = $this->get_value($str, $arr);
        return $new_str;
    }

    protected function get_value($str, $arr)
    {
        $_toarr = explode('/',$str);
        $count = count($_toarr);
        $new_str = '';
        for ($i = 0;$i < $count;$i++){

            if (isset($arr[$_toarr[$i]])){
                $new_str .= $arr[$_toarr[$i]]."/";
            }
        }

        return trim($new_str, '/');
    }

    // 获取复选框的键
    protected function get_key($str, $array)
    {
        $arr = explode(',', $str);

        $len = count($arr);
        $key_str = '';
        for ($i = 0; $i < $len; $i++) {
            $key_str .= array_search($arr[$i], $array) . ',';
        }
        return trim($key_str, ',');
    }
}
